<?php
/*
 * Created on 2012-9-6
 * Creater :cnluckylee
 * Creater mail:cnlucklee@gmail.com
 */
  class Search_model extends CI_Model{
 	public function __construct()
 	{
 		$this->load->database();
 		$this->load->library('session');
 		$this->load->library('Tools');
 	}
 	public function showSearch($sday,$eday)
 	{
	 	$int_sdate = strtotime($sday);

		$int_edate = strtotime($eday." +1 day");

		$where = " where wid=".$this->session->userdata('wid')."  and searchEngine!=''  and arriveTime>=".$int_sdate." and arriveTime<=".$int_edate;
		$sql = "SELECT sum(stopTime) as stopTimes,count(arriveCount) as total_arrive,searchEngine,sum(isbad)".
				" as badclicks,sum(if(firstTime>(UNIX_TIMESTAMP()-86400),1,0)) as isnew,keyword,ip,vid from tj_stat ".
					$where .
				" GROUP BY searchEngine,keyword,vid,ip";
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		$data = array();

		foreach($arr as $kk=>$vv)
		{
			$key = $vv['searchEngine'];
			@$data[$key]['tmp_keywords'][]= $vv['keyword'];
			@$data[$key]['tmp_vids'][]= $vv['vid'];
			@$data[$key]['tmp_ips'][]= $vv['ip'];
			@$data[$key]['total_stopTimes'] +=$vv['stopTimes'];
			@$data[$key]['total_arrives'] +=$vv['total_arrive'];
			@$data[$key]['total_isnew'] +=$vv['isnew'];
		}
		if($data)
		{

			foreach($data as $k=>$v)
			{
				$data[$k]['total_keywords'] = count(array_unique($v['tmp_keywords']));
				$data[$k]['avg_arriveTimes'] = round($v['total_stopTimes']/$v['total_arrives'],2);
				$data[$k]['total_ips'] = count(array_unique($v['tmp_ips']));
				$data[$k]['total_vids'] = count(array_unique($v['tmp_vids']));
				$data[$k]['avg_searchTimes'] = round($data[$k]['total_keywords']/$data[$k]['total_vids'],2)*100;
				unset($data[$k]['tmp_keywords']);
				unset($data[$k]['tmp_ips']);
				unset($data[$k]['tmp_vids']);
			}
		}
		$caption_name = "搜索引擎统计图";
		$xml = $this->make_xml($data,$caption_name,'total_keywords');
		$result_arr = array();

		$result_arr['data'] = $data;
		$result_arr['xml'] = $xml;

		return $result_arr;
 	}
 	/**
 	 * 按关键词划分
 	 */
 	public function showKeyword($sday,$eday)
 	{
		$int_sdate = strtotime($sday);

		$int_edate = strtotime($eday." +1 day");
		$where = " and (keyword!='inner' and keyword!='地址栏进入')";
		$sql = "SELECT sum(stopTime) as stopTimes,count(arriveCount) as total_arrive,searchEngine,sum(isbad)".
				" as badclicks,sum(if(firstTime>(UNIX_TIMESTAMP()-86400),1,0)) as isnew,keyword,ip,vid from tj_stat where  ".
				" wid=".$this->session->userdata('wid')." and arriveTime>=".$int_sdate." and arriveTime<=".$int_edate.$where.
				" GROUP BY keyword,searchEngine,vid,ip";
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		$data = array();
		$total_arrives = 0;
		foreach($arr as $kk=>$vv)
		{
			$key = $vv['keyword'];
			@$data[$key]['tmp_searchEngine'][]= $vv['searchEngine'];
			@$data[$key]['tmp_vids'][]= $vv['vid'];
			@$data[$key]['tmp_ips'][]= $vv['ip'];
			@$data[$key]['total_stopTimes'] +=$vv['stopTimes'];
			@$data[$key]['total_arrives'] +=$vv['total_arrive'];
			$total_arrives +=$vv['total_arrive'];
			@$data[$key]['total_isnew'] +=$vv['isnew'];
			@$data[$key]['total_arrive'] = $vv['total_arrive'];
		}
		if($data)
		{

			foreach($data as $k=>$v)
			{
				$data[$k]['total_searchEngine'] = count(array_unique($v['tmp_searchEngine']));
				$data[$k]['avg_arriveTimes'] = round($v['total_stopTimes']/$v['total_arrives'],2);
				$data[$k]['total_ips'] = count(array_unique($v['tmp_ips']));
				$data[$k]['total_vids'] = count(array_unique($v['tmp_vids']));
				$data[$k]['avg_searchTimes'] = round($data[$k]['total_searchEngine']/$data[$k]['total_vids'],2)*100;
				$data[$k]['avg_arrives'] = round($v['total_arrive']*100/$total_arrives,2);
				unset($data[$k]['tmp_searchEngine']);
				unset($data[$k]['tmp_ips']);
				unset($data[$k]['tmp_vids']);
			}
		}
		$caption_name = "关键词统计图";
		$xml = $this->make_xml($data,$caption_name,'total_searchEngine');
		$result_arr = array();
		$result_arr['data'] = $data;
		$result_arr['xml'] = $xml;

		return $result_arr;
	}
 	function make_xml($data,$caption_name,$value_name)
 	{


		$str = '<?xml version="1.0" encoding="gb2312"?>';
		$str .= '<chart caption="'.$caption_name.'" baseFontSize="14" palette="2" animation="1" formatNumberScale="0" pieSliceDepth="30" startingAngle="125" showValues="1" showPercentInToolTip="1" showLabels="1" showLegend="1" formatNumber="1">';
		foreach($data as $k=>$v)
		{
			$str .= '<set label="'.$k.'" value="'.$v[$value_name].'" isSliced="1"/>';
		}
		$str .='</chart>';
		return $str;
	}
  }
?>
